C++流操作之fstream用法介绍

 更新时间:2013年09月17日 15:44:33   作者:  
这篇文章详细介绍了C++流操作之fstream的用法,有需要的朋友可以参考一下

在Windows平台对文件进行存取操作可选的方案有很多,如果采用纯C,则需要用到File*等,当然也可以直接调用Windows API来做;如果采用C++,首先想到的就是文件流fstream。虽然在COM层面上,我们还可以使用IStream来实现文件的读写,其效率也非常高。不过本文仅对C++流操作做简单的探讨,相比于Windows API或IStream,C++的流操作通用性更好一些,因为你能轻松将代码移植到其它平台上。

fstream有两个派生类,即ifstream和ofstream,分别对应输入文件流、输出文件流。在使用它们之前,必须将它们的头文件包含到你的cpp文件中。

创建一个文件流的方法很简单:

ifstream fin; 
fin.open("C:\filename.txt"); 
这样就创建了一个输入文件流fin,它对应的文件是C盘根目录下的filename.txt。实际上,open方法还包含一个参数mode,用以指定其打开方式。
ios::in 以读取方式打开文件
ios::out 以写入方式打开文件
ios::ate 存取指针在文件末尾
ios::app 写入时采用追加方式
ios::trunc 写入时抹去旧数据
ios::binary 以二进制方式存取
上面的代码并未指定任何打开方式,则采用默认参数:输入文件流即ios::in,输出文件流即ios::out。一般在需要组合特殊的mode才显式指定,比如:
ios::in | ios::binary; //以二进制方式读取文件

除此之外,还可以在构造时指定相应的文件路径和名称,让创建过程一步到位。上述代码可改写为:

ifstream fin("C:\filename.txt");
与open方法相反的是close方法,它的作用与open正好相反。open是将文件流对象与外设中的文件关联起来,close则是解除二者的关联。但是需要注意的是,close还起到清空缓存的作用。最好让open方法与close方法成对出现。

创建并打开一个文件流后,就能像操作标准I/O那样使用流插入操作符(<<)与流提取操作符(>>)。对于输入文件流来说,可以调用getline函数从文件流中读取一整行数据,这样就可以读入含有空格的字符串。

下面是一个例子,该例的作用是读取一个STLA格式的文件。STL是一种常用快速成像文件格式,其格式非常简单,特别是ASCII版本(即STLA)。代码如下所示:

stdafx.h

复制代码 代码如下:

// stdafx.h : include file for standard system include files, 
// or project specific include files that are used frequently, but 
// are changed infrequently 
// 

#pragma once 

#include "targetver.h" 

#include <stdio.h> 
#include <tchar.h> 
//added 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <string> 
#include <vector> 
using namespace std; 

// TODO: reference additional headers your program requires here 
readstla.cpp

// readstla.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 

struct facet { 
    float normal[3]; 
    float vertex[3][3]; 
}; 

int _tmain(int argc, _TCHAR* argv[]) 

    if (argc < 2) { 
        printf("specify an input file!\n"); 
        return 1; 
    } 
    ifstream in(argv[1]); 
    if (!in.is_open()) { 
        printf("fail to open file!\n"); 
        return 1; 
    } 
    //var 
    vector<facet> solid; 
    string line; 
    string word; 
    //check format 
    getline(in, line); 
    if (line.find("solid") != 0) { 
        printf("wrong file format!\n"); 
        in.close(); 
        return 1; 
    } 
    while (getline(in, line)) { 
        if (line.find("facet normal") != string::npos) { 
            facet f; 
            //read normal 
            stringstream ns(line); 
            ns >> word; //eat "facet" 
            ns >> word; //eat "normal" 
            ns >> f.normal[0] >> f.normal[1] >> f.normal[2]; 
            //read vertices 
            getline(in, line); //"outer loop" 
            for (int i = 0; i < 3; i++) { 
                getline(in, line); 
                stringstream vs(line); 
                vs >> word; //eat "vertex" 
                vs >> f.vertex[i][0] >> f.vertex[i][1] >> f.vertex[i][2]; 
            } 
            getline(in, line); //"endloop" 
            getline(in, line); //"endfacet" 
            solid.push_back(f); 
        } 
    } 
    in.close(); 
    //output 
    int cnt = solid.size(); 
    printf("read %d facet\n", cnt); 
    for (int i = 0; i < cnt; i++) { 
        facet& f = solid[i]; 
        printf("\nfacet %d:\nnormal = (%f, %f, %f)\n", \ 
                       i+1, f.normal[0], f.normal[1], f.normal[2]); 
        for (int j = 0; j < 3; j++) { 
            printf("vertex[%d] = (%f, %f, %f)\n", \ 
                              j+1, f.vertex[j][0], f.vertex[j][1], f.vertex[j][2]); 
        } 
    } 
    return 0; 
}

测试文件为:
cube_corner.stl
复制代码 代码如下:


solid cube_corner 
  facet normal 0.0 -1.0 0.0 
    outer loop 
      vertex 0.0 0.0 0.0 
      vertex 1.0 0.0 0.0 
      vertex 0.0 0.0 1.0 
    endloop 
  endfacet 
  facet normal 0.0 0.0 -1.0 
    outer loop 
      vertex 0.0 0.0 0.0 
      vertex 0.0 1.0 0.0 
      vertex 1.0 0.0 0.0 
    endloop 
  endfacet 
  facet normal 0.0 0.0 -1.0 
    outer loop 
      vertex 0.0 0.0 0.0 
      vertex 0.0 0.0 1.0 
      vertex 0.0 1.0 0.0 
    endloop 
  endfacet 
  facet normal 0.577 0.577 0.577 
    outer loop 
      vertex 1.0 0.0 0.0 
      vertex 0.0 1.0 0.0 
      vertex 0.0 0.0 1.0 
    endloop 
  endfacet 
endsolid 

输入结果为:

read 4 facet 

facet 1: 

normal = (0.000000, -1.000000, 0.000000) 
vertex[1] = (0.000000, 0.000000, 0.000000) 
vertex[2] = (1.000000, 0.000000, 0.000000) 
vertex[3] = (0.000000, 0.000000, 1.000000) 

facet 2: 

normal = (0.000000, 0.000000, -1.000000) 
vertex[1] = (0.000000, 0.000000, 0.000000) 
vertex[2] = (0.000000, 1.000000, 0.000000) 
vertex[3] = (1.000000, 0.000000, 0.000000) 

facet 3: 
normal = (0.000000, 0.000000, -1.000000) 
vertex[1] = (0.000000, 0.000000, 0.000000) 
vertex[2] = (0.000000, 0.000000, 1.000000) 
vertex[3] = (0.000000, 1.000000, 0.000000) 

facet 4: 
normal = (0.577000, 0.577000, 0.577000) 
vertex[1] = (1.000000, 0.000000, 0.000000) 
vertex[2] = (0.000000, 1.000000, 0.000000) 
vertex[3] = (0.000000, 0.000000, 1.000000) 
Press any key to continue . . . 

相关文章

  • C++的optional用法实例详解

    C++的optional用法实例详解

    编程中我们可能会遇到要处理可能为空的变量,比如说容器,基本类型,或者说对象实例,下面通过实例代码介绍C++的optional用法,感兴趣的朋友一起看看吧
    2024-02-02
  • C语言版三子棋游戏实现代码

    C语言版三子棋游戏实现代码

    这篇文章主要为大家详细介绍了C语言版三子棋游戏的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • C语言模式实现C++继承和多态的实例代码

    C语言模式实现C++继承和多态的实例代码

    本篇文章主要介绍了C语言模式实现C++继承和多态的实例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • C++ stack与queue使用方法详细讲解

    C++ stack与queue使用方法详细讲解

    stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行 元素的插入与提取操作;队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中操作,其中从容器一端插入元素,另一端提取元素
    2023-01-01
  • C++常用字符串函数大全(2)

    C++常用字符串函数大全(2)

    这篇文章主要给大家分享的是C++常用字符串函数的大全,cstring.h库即C语言中的string.h库,它是C语言中为字符串提供的标准库。C++对此进行了兼容,所以我们在C++当中一样可以使用,下面文章的详细内容,需要的朋友可以参考一下
    2021-11-11
  • C++ 如何实现顺序栈(使用模板类)

    C++ 如何实现顺序栈(使用模板类)

    这篇文章主要介绍了C++ 如何实现顺序栈(使用模板类),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 深入jaxb xjc编码问题的详细介绍

    深入jaxb xjc编码问题的详细介绍

    本篇文章是对jaxb xjc编码的问题进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C++中的explicit关键字详解

    C++中的explicit关键字详解

    这篇文章主要介绍了C++中的explicit关键字详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下
    2022-07-07
  • C语言实现对文件进行操作的示例详解

    C语言实现对文件进行操作的示例详解

    这篇文章主要为大家详细介绍了C语言实现对文件进行操作的相关知识,文中的示例代码讲解详细,对我们学习C语言有一定的帮助,需要的可以参考一下
    2023-04-04
  • 详解C语言中结构体的自引用和相互引用

    详解C语言中结构体的自引用和相互引用

    这篇文章主要介绍了C语言中结构体的自引用和相互引用,详细解析了结构体中指针的指向情况,需要的朋友可以参考下
    2016-04-04

最新评论